package com.southminority.ethnic.controller.contact;

import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.service.contact.ContactInfoService;
import com.southminority.ethnic.controller.contact.vo.ContactInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 联系信息前端API控制器
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "📞 联系信息展示", description = "获取实验室联系方式、地址、电话等信息的前端接口")
@RestController
@RequestMapping("/api/contact-info")
@CrossOrigin
public class ContactInfoController {

    private final ContactInfoService contactInfoService;

    public ContactInfoController(ContactInfoService contactInfoService) {
        this.contactInfoService = contactInfoService;
    }

    @GetMapping("/list")
    @Operation(
        summary = "获取公开联系信息列表", 
        description = "获取所有公开显示的联系信息，包括地址、电话、邮箱、网站等"
    )
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(
                schema = @Schema(type = "array", implementation = ContactInfoVO.class),
                examples = @ExampleObject(
                    name = "联系信息列表示例",
                    description = "包含地址、电话、邮箱等联系方式",
                    value = """
                    [
                      {
                        "id": 1,
                        "type": "address",
                        "typeDisplay": "地址信息",
                        "title": "实验室地址",
                        "icon": "fas fa-map-marker-alt",
                        "contentList": [
                          "贵州省贵阳市花溪区",
                          "贵州民族大学花溪校区文科楼510室",
                          "邮编: 550025"
                        ],
                        "description": "南方少数民族语言信息化实验室办公地址",
                        "isPrimary": true,
                        "isPublic": true,
                        "displayOrder": 1
                      }
                    ]
                    """
                )
            ))
    })
    public R<List<ContactInfoVO>> getPublicContactInfoList() {
        try {
            List<ContactInfoVO> contactInfoList = contactInfoService.getPublicContactInfoList();
            return R.ok(contactInfoList);
        } catch (Exception e) {
            log.error("获取公开联系信息列表失败", e);
            return R.fail("获取联系信息失败: " + e.getMessage());
        }
    }

    @GetMapping("/type/{type}")
    @Operation(
        summary = "根据类型获取联系信息", 
        description = "根据联系方式类型获取对应的联系信息，支持地址、电话、邮箱、网站、社交媒体等类型"
    )
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(schema = @Schema(type = "array", implementation = ContactInfoVO.class))),
        @ApiResponse(responseCode = "400", description = "类型参数无效")
    })
    public R<List<ContactInfoVO>> getContactInfoByType(
            @Parameter(
                description = "联系方式类型", 
                example = "phone", 
                required = true,
                schema = @Schema(allowableValues = {"address", "phone", "email", "website", "social"})
            ) @PathVariable String type) {
        try {
            List<ContactInfoVO> contactInfoList = contactInfoService.getContactInfoByType(type);
            return R.ok(contactInfoList);
        } catch (Exception e) {
            log.error("根据类型获取联系信息失败: type={}", type, e);
            return R.fail("获取联系信息失败: " + e.getMessage());
        }
    }

    @GetMapping("/primary")
    @Operation(
        summary = "获取主要联系信息", 
        description = "获取标记为主要联系方式的信息，通常用于首页或关键位置展示"
    )
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(schema = @Schema(type = "array", implementation = ContactInfoVO.class)))
    })
    public R<List<ContactInfoVO>> getPrimaryContactInfo() {
        try {
            List<ContactInfoVO> contactInfoList = contactInfoService.getPrimaryContactInfo();
            return R.ok(contactInfoList);
        } catch (Exception e) {
            log.error("获取主要联系信息失败", e);
            return R.fail("获取主要联系信息失败: " + e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @Operation(
        summary = "获取联系信息详情", 
        description = "根据ID获取特定联系信息的详细信息"
    )
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(schema = @Schema(implementation = ContactInfoVO.class))),
        @ApiResponse(responseCode = "404", description = "联系信息不存在")
    })
    public R<ContactInfoVO> getContactInfoDetail(
            @Parameter(description = "联系信息ID", example = "1", required = true) 
            @PathVariable Long id) {
        try {
            ContactInfoVO contactInfo = contactInfoService.getContactInfoDetail(id);
            return R.ok(contactInfo);
        } catch (Exception e) {
            log.error("获取联系信息详情失败: id={}", id, e);
            return R.fail("获取联系信息详情失败: " + e.getMessage());
        }
    }

    @GetMapping("/test")
    @Operation(
        summary = "测试接口连通性", 
        description = "测试联系信息服务是否正常运行"
    )
    @ApiResponse(responseCode = "200", description = "服务正常",
        content = @Content(
            schema = @Schema(type = "string"),
            examples = @ExampleObject(value = "联系信息服务正常运行")
        ))
    public R<String> test() {
        return R.ok("联系信息服务正常运行");
    }
} 
